package com.czk.yygh.order.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czk.enums.OrderStatusEnum;
import com.czk.enums.PaymentStatusEnum;
import com.czk.enums.PaymentTypeEnum;
import com.czk.model.order.OrderInfo;
import com.czk.model.order.PaymentInfo;
import com.czk.vo.order.SignInfoVo;
import com.czk.yygh.common.helper.HttpRequestHelper;
import com.czk.yygh.hosp.client.HospitalFeignClient;
import com.czk.yygh.order.mapper.PaymentMapper;
import com.czk.yygh.order.service.OrderService;
import com.czk.yygh.order.service.PaymentService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/11/30 16:33
 */
@Service
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, PaymentInfo> implements PaymentService {
    @Autowired
    private OrderService orderService;
    @Autowired
    private HospitalFeignClient hospitalFeignClient;

    @Override
    public void savePaymentInfo(OrderInfo orderInfo, Integer status) {
        // 查看订单是否存在
        // 根据订单id和支付类型查询
        QueryWrapper<PaymentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderInfo.getId());
        wrapper.eq("payment_type", status);
        // 查询
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            return;
        }
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setPaymentType(status);
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        paymentInfo.setPaymentStatus(PaymentStatusEnum.UNPAID.getStatus());
        String subject = new DateTime(orderInfo.getReserveDate()).toString("yyyy-MM-dd") + "|" + orderInfo.getHosname() + "|" + orderInfo.getDepname() +
                "|" + orderInfo.getTitle();
        paymentInfo.setSubject(subject);
        paymentInfo.setTotalAmount(orderInfo.getAmount());
        baseMapper.insert(paymentInfo);
    }

    @Override
    public void paySuccess(String out_trade_no, Map<String, String> resultMap) {
        // 得到支付信息
        QueryWrapper<PaymentInfo> wrapper = new QueryWrapper<>();
        // 订单号
        wrapper.eq("out_trade_no", out_trade_no);
        // zhifuleixing
        wrapper.eq("payment_type", PaymentTypeEnum.WEIXIN.getStatus());
        //
        PaymentInfo paymentInfo = baseMapper.selectOne(wrapper);
        // 设置支付值
        paymentInfo.setPaymentStatus(PaymentStatusEnum.PAID.getStatus());
        // 设置回调时间
        paymentInfo.setCallbackTime(new Date());
        // 微信支付订单号
        paymentInfo.setTradeNo(resultMap.get("transaction_id"));
        paymentInfo.setCallbackContent(resultMap.toString());
        baseMapper.updateById(paymentInfo);

        // 得到订单信息
        OrderInfo orderInfo = orderService.getById(paymentInfo.getOrderId());
        // 设置已支付
        orderInfo.setOrderStatus(OrderStatusEnum.PAID.getStatus());
        // 更新
        orderService.updateById(orderInfo);
        // 通知医院支付完成
        SignInfoVo signInfoVo = hospitalFeignClient.getSignInfoVo(orderInfo.getHoscode());
        // 封装参数
        Map<String, Object> reqMap = new HashMap<>();
        reqMap.put("hoscode", orderInfo.getHoscode());
        reqMap.put("hosRecordId", orderInfo.getHosRecordId());
        reqMap.put("timestamp", HttpRequestHelper.getTimestamp());
        String sign = HttpRequestHelper.getSign(reqMap, signInfoVo.getSignKey());
        reqMap.put("sign", sign);
        JSONObject result = HttpRequestHelper.sendRequest(reqMap, signInfoVo.getApiUrl() + "/order/updatePayStatus");
    }

    @Override
    public PaymentInfo getPaymentInfo(Long orderId, Integer paymentType) {
        QueryWrapper<PaymentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderId);
        wrapper.eq("payment_type", paymentType);
        PaymentInfo paymentInfo = baseMapper.selectOne(wrapper);
        return paymentInfo;
    }
}
